package cn.itcast.leadnews.service.impl;
import cn.itcast.leadnews.common.AppHttpCodeEnum;
import cn.itcast.leadnews.common.PageResponseResult;
import cn.itcast.leadnews.common.ResponseResult;
import cn.itcast.leadnews.dto.WmNewsDTO;
import cn.itcast.leadnews.exception.CustException;
import cn.itcast.leadnews.mapper.WmNewsMapper;
import cn.itcast.leadnews.pojo.WmNews;
import cn.itcast.leadnews.service.WmNewsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
@Slf4j
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {
    @Override
    public PageResponseResult findList(WmNewsDTO dto) {
        // 1. 校验参数  分页   登陆用户
        dto.checkParam();
        // 2. 获取当前登陆用户id   登陆用户id 写死:
        Integer userId  = 1102;
        // 2. 执行查询
        // 2.1 封装分页查询条件
        Page<WmNews> pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 2.2 封装参数查询条件
        LambdaQueryWrapper<WmNews> wrapper = Wrappers.lambdaQuery();
        //       如果有文章标题  模糊查询
        String keyword = dto.getKeyword();
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like(WmNews::getTitle,keyword);
        }
        //       如果有频道id    eq查询
        Integer channelId = dto.getChannelId();
        if (channelId!=null) {
            wrapper.eq(WmNews::getChannelId,channelId);
        }
        //       如果有文章状态    eq查询
        Integer status = dto.getStatus();
        if (status!=null) {
            wrapper.eq(WmNews::getStatus,status);
        }
        //       如果有开始时间和结束时间   发布时间范围查询
        Date beginPubDate = dto.getBeginPubDate();
        Date endPubDate = dto.getEndPubDate();
        if(beginPubDate!=null && endPubDate!=null){
            wrapper.between(WmNews::getPublishTime,beginPubDate,endPubDate);
        }
        //       按照登陆用户id    eq查询
        wrapper.eq(WmNews::getUserId,userId);
        //       按照发布时间降序排序
        wrapper.orderByDesc(WmNews::getPublishTime);
        // 2.3 执行查询得到结果
        IPage<WmNews> pageResult = page(pageReq, wrapper);
        // 3. 封装分页结果返回   记得设置host字段 ==> webSite
        return new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }
    @Override
    public ResponseResult delNews(Integer id) {
        //1.检查参数
        if(id == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"文章Id不可缺少");
        }
        //2.获取数据
        WmNews wmNews = getById(id);
        if(wmNews == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
        }

        if (wmNews.getStatus().intValue() == 9
                && wmNews.getEnable().intValue() == 0) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"发布上架文章不可以删除哟");
        }
        //5.删除文章 (逻辑删除)
        removeById(wmNews.getId());
        return ResponseResult.okResult();
    }
    /**
     * 修改自媒体文章状态
     * @param status 2  审核失败  4 审核成功
     * @return
     */
    @Override
    public ResponseResult updateStatus(Integer status, Integer newsId) {

        //2.查询文章
        WmNews wmNews = getById(newsId);
        if(wmNews == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.修改文章状态
        wmNews.setStatus(status);
        updateById(wmNews);
        return ResponseResult.okResult();
    }
}
